package com.qimou.iems.controller;

import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.QualityCategory;
import com.qimou.iems.entity.QualityCategoryChild;
import com.qimou.iems.service.IQualityCategoryChildService;
import com.qimou.iems.util.CommonUtil;
import com.qimou.iems.util.StatusConstant;
import com.qimou.iems.util.ViewData;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/6/9 10:52.
 **/

@RestController
@RequestMapping(value = "categoryChild")
public class QualityCategoryChildController extends BaseController {

    @Resource
    private IQualityCategoryChildService qualityCategoryChildService;



    /**
     * 获取列表
     */
    @RequestMapping(value = "/getChildList")
    public ViewData getChildList(String childName,Integer categoryId){
        return buildSuccessJson(StatusConstant.SUCCESS_CODE,"获取成功",
                qualityCategoryChildService.getChildList(categoryId,childName));
    }


    /**
     * 获取列表
     */
    @RequestMapping(value = "/getList")
    public ViewData getList(PageArgs pageArgs, String childName,Integer categoryId){
        childName = CommonUtil.isEmpty(childName) ? null : childName;
        categoryId = CommonUtil.isEmpty(categoryId) ? null : categoryId;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("childName",childName);
        map.put("categoryId",categoryId);
        PageList<QualityCategoryChild> data = qualityCategoryChildService.getList(map,pageArgs);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                data.getTotalSize(),data.getList());
    }


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ViewData save(String childName,Integer categoryId){
        if(CommonUtil.isEmpty(categoryId,childName)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数不能为空");
        }
        QualityCategoryChild child = new QualityCategoryChild();
        child.setCategoryId(categoryId);
        child.setChildName(childName);
        qualityCategoryChildService.save(child);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ViewData update(QualityCategoryChild child){
        if(null != child.getId()){
            qualityCategoryChildService.update(child);
        }
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


}
